﻿using CaroLibraly;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Threading.Tasks;

namespace CaroServer
{
    public class ServerNetwork
    {
        private static HttpChannel channnel;

        public static bool Start()
        {
            try
            {
                SoapClientFormatterSinkProvider clientProvider = new SoapClientFormatterSinkProvider();
                SoapServerFormatterSinkProvider serverProvider = new SoapServerFormatterSinkProvider();

                serverProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
                IDictionary props = new Hashtable();
                props["port"] = InfoServer.Port;
                props["typeFilterLevel"] = TypeFilterLevel.Full;

                channnel = new HttpChannel(props, clientProvider, serverProvider);
                ChannelServices.RegisterChannel(channnel, false);

                WellKnownServiceTypeEntry w = new WellKnownServiceTypeEntry(
                    InfoServer.NameClass
                    , InfoServer.NameSpace
                    , InfoServer.ServerName
                    , WellKnownObjectMode.Singleton
                    );
                RemotingConfiguration.RegisterWellKnownServiceType(w);
                RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void Stop()
        {
            try
            {
                ChannelServices.UnregisterChannel(channnel);
            }
            catch
            {
            }
        }
    }
}
